package com.huang.core.widget.edittext

import android.text.Editable
import com.huang.core.util.NumberUtil

class MaxTextChangeListener private constructor() : SimpleTextChangeListener() {

    private var dMaxCount: Double? = null
    private var fMaxCount: Float? = null
    private var lMaxCount: Long? = null
    private var iMaxCount: Int? = null
    private var sMaxCount: Short? = null

    constructor(maxCount: Double) : this() {
        this.dMaxCount = maxCount
    }

    constructor(maxCount: Float) : this() {
        this.fMaxCount = maxCount
    }

    constructor(maxCount: Long) : this() {
        this.lMaxCount = maxCount
    }

    constructor(maxCount: Int) : this() {
        this.iMaxCount = maxCount
    }

    constructor(maxCount: Short) : this() {
        this.sMaxCount = maxCount
    }

    override fun afterTextChanged(s: Editable) {
        super.afterTextChanged(s)
        if (dMaxCount != null) {
            if (NumberUtil.string2Double(s.toString()) > dMaxCount!!) {
                s.replace(0, s.length, "$dMaxCount")
            }
        } else if (fMaxCount != null) {
            if (NumberUtil.string2Float(s.toString()) > fMaxCount!!) {
                s.replace(0, s.length, "$fMaxCount")
            }
        } else if (lMaxCount != null) {
            if (NumberUtil.string2Long(s.toString()) > lMaxCount!!) {
                s.replace(0, s.length, "$lMaxCount")
            }
        } else if (iMaxCount != null) {
            if (NumberUtil.string2Int(s.toString()) > iMaxCount!!) {
                s.replace(0, s.length, "$iMaxCount")
            }
        } else if (sMaxCount != null) {
            if (NumberUtil.string2Short(s.toString()) > sMaxCount!!) {
                s.replace(0, s.length, "$sMaxCount")
            }
        }


        afterFilter(s)
    }

    open fun afterFilter(s: Editable) {

    }
}